﻿    using System;
    using System.Collections.Generic;
    using UnityEngine;

    namespace TG
    {

        [Serializable]
        public class MapCellArray
        {
            /// <summary>
            /// 当前物品地图组
            /// </summary>
            public int[][] mapArray = new int[3][];

            /// <summary>
            /// 不可生成物品地图组
            /// </summary>
            private int[][] cantUseMapCells = new int[5][];

            /// <summary>
            /// 不可生成物品地图组
            /// </summary>
            public List<MapCell> cantUseMapCellList = new List<MapCell>();

            /// <summary>
            /// 地图组标识
            /// </summary>
            public int arrayIndex = -1;

            /// <summary>
            /// 返回地图组物品
            /// </summary>
            /// <returns></returns>
            public int GetMapCellType()
            {
                return arrayIndex;
            }

            /// <summary>
            /// 一个格子生成物品后 格子组
            /// </summary>
            /// <param name="mapIndex"></param>
            public void InitItemCantUserMapCellArray(int mapIndex)
            {
                arrayIndex = mapIndex;
                int mapArrayChangeValue = 0;
                int changeValue = 0;
                for (int i = 0; i < 5; i++)
                {
                    if (i < 3)
                    {
                        mapArrayChangeValue = mapIndex + (i - 1) * Default.MapCellWidthNum;
                        mapArray[i] = new int[3];
                    }

                    cantUseMapCells[i] = new int [5];
                    changeValue = mapIndex + (i - 2) * Default.MapCellWidthNum;
                    for (int j = 0; j < 5; j++)
                    {
                        if (i < 3 && j < 3)
                        {
                            int tempMapArrayChangeValue = mapArrayChangeValue + j - 1;
                            mapArray[i][j] = tempMapArrayChangeValue;
                        }

                        int tempChangeValue = changeValue + j - 2;
                        cantUseMapCells[i][j] = tempChangeValue;
                    }
                }

                Debug.Log($"打印九宫格坐标");
                ChangeArrayStandardValue(mapIndex, mapArray, 1);
                Debug.Log($"打印25宫格坐标");
                ChangeArrayStandardValue(mapIndex, cantUseMapCells, 2, cantUseMapCellList);
            }

            /// <summary>
            /// 格子组取值标准化
            /// </summary>
            private void ChangeArrayStandardValue(int index, int[][] array, int sub, List<MapCell> list = null)
            {
                //当前物品排的第一个
                int lineStartIndex = index - (index % Default.MapCellWidthNum);
                //当前物品排的最后一个
                int lineEndIndex = lineStartIndex + Default.MapCellWidthNum - 1;

                int maxIndex = Default.MapCellWidthNum * Default.MapCellWidthNum;

                for (int i = 0; i < array.Length; i++)
                {
                    //i排的第一个
                    var iLineStart = lineStartIndex + (i - sub) * Default.MapCellWidthNum;
                    //i排的最后一个
                    var iLineEnd = lineEndIndex + (i - sub) * Default.MapCellWidthNum;
                    var mapCellArray = array[i];
                    for (int j = 0; j < mapCellArray.Length; j++)
                    {
                        if (mapCellArray[j] < iLineStart || mapCellArray[j] > iLineEnd || mapCellArray[j] >= maxIndex)
                            mapCellArray[j] = -1;
                        else if (list != null && mapCellArray[j] >= 0)
                        {
                            var mapCell = Map.Instance.GetMapCell(mapCellArray[j]);
                            if (mapCell != null)
                            {
                                mapCell.useNum++;
                                list.Add(mapCell);
                            }
                        }
                    }
                }
            }

            public bool ContainsMapCell(int mapIndex)
            {
                foreach (var mapCell in cantUseMapCellList)
                {
                    if (mapCell.mapItemId == mapIndex)
                    {
                        return true;
                    }
                }

                return false;
            }

            public void Clear()
            {
                mapArray = null;
                cantUseMapCells = null;
                cantUseMapCellList.Clear();
                arrayIndex = -1;
            }
        }
    }
